using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Typing_GUI
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        //private void btnNewUser_Click(object sender, EventArgs e)
        //{
        //    frmSignUp frm = new frmSignUp();
        //    frm.FormClosed += new FormClosedEventHandler(MakeVisibleAgain);
        //    frm.Show();
        //    this.Visible = false;
        //}

        private void frmLogin_Load(object sender, EventArgs e)
        {
           // this.BackgroundImage = new Bitmap("images/tomnjerryframe.jpg");
            frmWelcome frm = new frmWelcome();
            frm.ShowDialog();
        }


        char[] specialChar ={ '+', '-', '.', ',', '/', '?', '"', ':', '\\', ';', '=', '\'', '`', '~', '@', '#', '$', '%', '^', '&', '*', '(', ')', ' ', '|' };

        bool KiemTraUsename()
        {
            if (txtUsername.Text.Length == 0)
                return false;
            for (int i = 0; i < txtUsername.Text.Length; i++)
            {
                for (int j = 0; j < specialChar.Length; j++)
                {
                    if (txtUsername.Text[i] == specialChar[j])
                        return false;
                }
            }
            return true;
        }
        private void MakeVisibleAgain(object sender, FormClosedEventArgs e)
        {
            this.Visible = true;
          //  this.Close();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {

            if (chkGuest.Checked)
            {

                if (chkGuest.Checked == true)
                {
                    frmGuestPlay frm = new frmGuestPlay();
                    frm.FormClosed += new FormClosedEventHandler(MakeVisibleAgain);
                    frm.Show();
                    this.Visible = false;
                }
              
            }
                else
                {
                    if (KiemTraUsename())
                    {
                        int loaius = 0;
                        if (CLASS.CNguoiDung.KiemTraNguoiDung(txtUsername.Text, txtPassword.Text, ref loaius))
                        {
                            if (CLASS.CNguoiDung.KiemTraBannick(txtUsername.Text))
                            {
                                MessageBox.Show("Your account was prevented!", "Message");
                                return;
                            }
                            if (loaius == 1)
                            {
                                frmAdmin frm = new frmAdmin();
                                frm.FormClosed += new FormClosedEventHandler(MakeVisibleAgain);
                                frm.userID = CLASS.CNguoiDung.GetUserID(txtUsername.Text);
                                frm.Show();
                                this.Visible = false;

                                txtUsername.Text = "";
                                txtPassword.Text = "";
                                txtUsername.Focus();
                            }
                            if (loaius == 2)
                            {
                                frmUser frm = new frmUser();
                                frm.userID = CLASS.CNguoiDung.GetUserID(txtUsername.Text);
                                frm.FormClosed += new FormClosedEventHandler(MakeVisibleAgain);
                                frm.Show();
                                this.Visible = false;

                                txtUsername.Text = "";
                                txtPassword.Text = "";
                                txtUsername.Focus();
                            }
                        }
                        else
                        {
                            MessageBox.Show("Invalid login ! Please re-enter !!!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        
                            txtUsername.Focus();
                            txtUsername.SelectAll();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Invalid login ! Please re-enter !!!", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        
                        txtUsername.Focus();
                        txtUsername.SelectAll();

                    }
                }
            
          
        }

        private void chkGuest_CheckedChanged(object sender, EventArgs e)
        {
            if (chkGuest.Checked)
            {
                txtUsername.Text = "";
                txtPassword.Text = "";
                txtUsername.Enabled = false;
                txtPassword.Enabled = false;
                
            }
            else
            {
                txtUsername.Enabled = true;
                txtPassword.Enabled = true;
                txtUsername.Focus();
            }
        }

        private void txtPassword_Enter(object sender, EventArgs e)
        {
            txtPassword.SelectAll();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void llblNewUser_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            frmSignUp frm = new frmSignUp();
            frm.FormClosed += new FormClosedEventHandler(MakeVisibleAgain);
            frm.Show();
            this.Visible = false;
        }
    }
}